// 加载JS, 不能直接包含js文件时调用
function loadjs( url, urlCallback, fnCallback ) {
	var scriptElem = document.createElement("script"),
		head = document.getElementsByTagName("head")[0] || document.documentElement.firstChild;
	
	if ( scriptElem.readyState ) {
		// IE浏览器
		scriptElem.onreadystatechange = function () {
			if ( scriptElem.readyState === "loaded" || scriptElem.readyState === "complete" ) {
				scriptElem.onreadystatechange = null;
				urlCallback && urlCallback.call(this, fnCallback);
			}
		};
	} else {
		// 其它浏览器
		scriptElem.onload = function () {
			urlCallback && urlCallback.call(this, fnCallback);
			scriptElem.onload = null;
		};
	}

	scriptElem.src = url;
	head.appendChild(scriptElem);
};

// 渲染广告
function appendAD( callback ) {
	if ( typeof(ninejs) === "object" ) {
		ninejs.extend(ninejs, function( exports ) {
			var NineAD = {
				initialize: function( options ) {
					if ( !options || !options.pic ) return;
					if ( !options.width ) options.width = 250;
					if ( !options.height ) options.height = 200;
					if ( !options.url || options.url == '' ) options.url = "http://www.duoqu.com/";
					if ( !options.title || options.title == '' ) options.title = "多趣网";
					if ( !options.closePic || options.closePic == '' ) options.closePic = "http://act.duoqu.com/static/images/close.gif";
					if ( !options.closeWidth ) options.closeWidth = 15;
					if ( !options.closeHeight ) options.closeHeight = 15;
					if ( !options.strADStyle ) {
						if ( exports.BrowserEntry.msie ) {
							options.strADStyle = {
								position: "absolute",
								zIndex: 9999999
							};
						} else {
							options.strADStyle = {
								position: "fixed",
								right: "10px",
								bottom: "10px",
								zIndex: 9999999
							};
						}				
					}
					if ( !options.strCloseStyle || options.strCloseStyle == ''  ) {
						options.strCloseStyle = "position: absolute;right: 0;top: -18px;";
					}

					for ( var i in options ) {
						if ( !this[i] ) {
							this[i] = options[i];
						}
					}

					this.render();
					this.fixPosition();
					this.addEvents();
				},
				render: function() {
					var strHtml = '';
					
					strHtml += '<a href="javascript:void(0);" id="DUOQU_AD_CLOSE" style="' + this.strCloseStyle + '" title="关闭">';
					strHtml += '	<img src="' + this.closePic + '" width="' + this.closeWidth + '" height="' + this.closeHeight + '" border="0" />';
					strHtml += '</a>';
					strHtml += '<a href="' + this.url + '" title="' + this.title + '" target="_blank">';
					strHtml += '	<img src="' + this.pic + '" width="' + this.width + '" height="' + this.height + '" alt="' + this.title + '" border="0" />';
					strHtml += '</a>';

					var div = document.createElement("div");
					div.id = "DUOQU_AD";
					for ( var i in this.strADStyle ) {
						if ( this.strADStyle.hasOwnProperty(i) ) {
							div.style[i] = this.strADStyle[i];
						}
					}
					div.innerHTML = strHtml;
					
					document.body.appendChild(div);
				},
				addEvents: function() {
					var elem = exports.getElemById("DUOQU_AD_CLOSE");

					exports.EventEntry.addEvent(window, "scroll", this.fixPosition);
					exports.EventEntry.addEvent(window, "resize", this.fixPosition);

					elem.onclick = this.close;
				},
				close: function() {
					var elem = exports.getElemById("DUOQU_AD");

					if ( elem ) {
						elem.style.display = "none";
					}							
				},
				fixPosition: function() {
					var _this = this;

					function _fixPostion() {
						var elem = exports.getElemById("DUOQU_AD"),
							st = exports.BrowserEntry.getScrollTop(),
							sl = exports.BrowserEntry.getScrollLeft(),
							ch = exports.BrowserEntry.getClientHeight(),
							cw = exports.BrowserEntry.getClientWidth(),
							sh = exports.BrowserEntry.getScrollHeight(),
							t = st + ch - _this.height - 10;

						if ( elem ) {
							elem.style.top = ((t + _this.height) >= sh ? (sh - _this.height) : t) + "px";
							elem.style.left = (sl + cw - _this.width - 10) + "px";
						}					
					}

					exports.BrowserEntry.msie && setTimeout(_fixPostion, 100);
				}
			};

			exports.NineAD = NineAD;
		});

		callback && callback.call(this, ninejs);
	}
}